import time
import os
from selenium.webdriver.support.wait import WebDriverWait
import win32gui
import win32con


def get_element(driver, fixture):
    wait = WebDriverWait(driver, 90, 0.5)
    try:
        ele = wait.until(lambda x: x.find_element(*fixture))
    except Exception:
        return None
    else:
        return ele


def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)


def get_path():
    base_dir = os.path.dirname(os.path.abspath(__file__))
    return base_dir


def upload(file_path, browser_type="chrome"):
    time.sleep(1)
    if browser_type == "chrome":
        title = "打开"
        time.sleep(1)
    else:
        title = ""
    # 找元素
    # 一级窗口 "#32770", "打开"
    dialog = win32gui.FindWindow("#32770", title)
    ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None)  # 二级窗口,0代表从第一个开始找,后面是class和title
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, "ComboBox", None)  # 三级窗口
    # 编辑按钮
    edit = win32gui.FindWindowEx(ComboBox, 0, "Edit", None)  # 四级窗口
    # 打开按钮
    time.sleep(2)
    button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&0)")
    time.sleep(2)
    # 往编辑当中输入文件路径
    win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)  # 发送文件路径
    time.sleep(2)
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)  # 点击打开按钮
    time.sleep(2)
